define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

	var Controller = {
		index  : function () {
			// 初始化表格参数配置
			Table.api.init({
				extend : {
					index_url  : 'qingdongams/parts/parts/index',
					add_url    : 'qingdongams/parts/parts/add',
					detail_url : 'qingdongams/parts/parts/detail',
					del_url    : 'qingdongams/parts/parts/del',
					table      : 'parts',
				}
			});

			var table = $("#table");
			var q = {};
			location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v);
			var isselect=false;
			if (q.isselect== 1) {
				isselect= true;
			}
			// 初始化表格
			table.bootstrapTable({
				url          : $.fn.bootstrapTable.defaults.extend.index_url,
				pk           : 'id',
				sortName     : 'id',
				fixedColumns : true,
				fixedNumber  : 2,
				columns      : [
					[
						{checkbox: true},
						{
							field: 'name', title: '备件名称', operate: "like", fixedColumns: true, formatter: function (value, row, index) {

								return "<a href='javascript:void(0);' data-id='" + row.id + "' class='show-detail'>" + value + "</a>";
							}
						},
						{field: 'num', title: '备件编号', operate: "like"},
						{field: 'img', title: '备件图片', formatter: Table.api.formatter.image, operate: false},
						{field: 'unit', title: '单位'},
						{field: 'stock', title: '剩余库存', operate: false, formatter: function (value, row, index) {
								if (row.stock < row.warning_stock){
									return "<span style='color: red'>" + value + "</span>";
								}else{
									return value;
								}
							}},
						{field: 'warning_stock', title: '预警库存', operate: false},
						{field: 'price', title: '标准价格', operate: false},
						{field: 'description', title: '描述'},
						{
							field: 'operate2', title: __('Operate'), events: {
								'click .btn-chooseone': function (e, value, row, index) {
									Fast.api.close({id: row.id, name: row.name});
								},
							},visible:isselect, formatter: function () {
								return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
							}
						},
						{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
							formatter: Table.api.formatter.buttons,visible:!isselect,
							buttons: [
								{
									name: 'edit',
									text: __('编辑'),
									title: __('编辑'),
									extend:'data-area=["90%","90%"]',
									classname: 'btn-xs records btn-dialog',
									url: 'qingdongams/parts/parts/edit',
									visible: function (row) {
										//返回true时按钮显示,返回false隐藏
										return true;
									}
								},
								{
									name: 'import',
									text: __('备件入库'),
									title: __('备件入库'),
									icon: 'fa fa-add',
									classname: 'btn-xs btn-chuku btn-dialog',
									url: 'qingdongams/parts/parts/import'+ location.search,
									extend: 'data-area=\'["70%","70%"]\'',
								},
								{
									name: 'export',
									text: __('备件出库'),
									title: __('备件出库'),
									icon: 'fa fa-add',
									classname: 'btn-xs btn-chuku btn-dialog',
									url: 'qingdongams/parts/parts/export'+ location.search,
									extend: 'data-area=\'["70%","70%"]\'',
								}
							]
						}
					]
				],
				onLoadSuccess:function(){
					// 这里就是数据渲染结束后的回调函数
					$(".btn-add").data("area",["90%","90%"]);
					$(".btn-edit").data("area",["90%","90%"]);
				}
			});


			$(document).on('click', '.show-detail', function (data) {
				var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
				var options = {
					shadeClose : false,
					shade      : [0.3, '#393D49'],
					area       : area,
					callback   : function (value) {
						//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
						console.log(value);
					}
				};
				Fast.api.open($.fn.bootstrapTable.defaults.extend.detail_url + "?ids=" + $(this).data('id'), '备件详情', options);
			});

			// 为表格绑定事件
			Table.api.bindevent(table);
		},
		add    : function () {
			Controller.api.bindevent();
		},
		edit   : function () {


			Controller.api.bindevent();
		},
		export   : function () {
			Controller.api.bindevent();
		},
		import   : function () {
			Controller.api.bindevent();
		},
		log_info   : function () {

			$('.btn-ajax').data("success", function(data, ret){
				//处理回调
				location.reload();
			})
			Controller.api.bindevent();
		},
		logs: function () {
			// 初始化表格参数配置
			Table.api.init({
				extend: {
					index_url: 'qingdongams/parts/parts/logs',
					table: 'examine'
				}
			});

			var table = $("#table");
			table.bootstrapTable({
				url: $.fn.bootstrapTable.defaults.extend.index_url,
				sortName: 'id',
				columns: [
					[
						{field: 'state', checkbox: true,},
						{
							field: 'parts.create_staff.name',
							title: __('提交人'),
							operate: false,
							searchList: $.getJSON("qingdongams/customer/customer/stafflist")
						},

						{
							field: 'parts.odd_numbers',
							title: '申请编号',
							fixedColumns: true,
							formatter: function (value, row, index) {

								return "<a href='javascript:void(0);' data-id='" + row.parts.id + "' class='show-log_info'>" + value + "</a>";
							},
							operate: false
						},
						{
							field: 'parts.type',
							title: '出库/入库',
							operate: false,
							formatter: Table.api.formatter.status,
							searchList: {1: __('入库'), 2: __('出库')}
						},
						{field: 'parts.storage_time', title: '出入库时间', operate: false},
						{field: 'check_staff.name', title: __('审批人'), operate: false},
						{
							field: 'status',
							title: __('审批状态'),
							operate: '=',
							formatter: Table.api.formatter.status,
							searchList: {0: __('待审批'), 1: __('审批通过'), 2: __('审批拒绝'), 3: __('撤销')}
						},
						{
							field: 'createtime',
							title: __('创建时间'),
							formatter: Table.api.formatter.datetime,
							operate: 'RANGE',
							addclass: 'datetimerange'
						},
					]
				],
				//启用普通表单搜索
				commonSearch: true,
				searchFormVisible: true,
			});
			$(document).on('click', '.show-log_info', function (data) {
				var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
				var options = {
					shadeClose: false,
					shade: [0.3, '#393D49'],
					area: area,
					end: function (value) {
						//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
						$(".btn-refresh").trigger("click");
					}
				};
				Fast.api.open("qingdongams/parts/parts/log_info?ids=" + $(this).data('id'), '出入库详情', options);
			});
		},
		detail : function () {
			Table.api.init({});
			//编辑
			$(document).on('click', '.btn-edit', function () {
				var id = $('#ids').val();
				Fast.api.open("qingdongams/parts/parts/edit?ids=" + id, "备件编辑", {
					shadeClose : false,
					shade      : false,
					maxmin     : false,
					moveOut    : false,
					scrollbars : false,
					area:["90%","90%"],
					callback   : function () {
						location.reload();
					}
				});

			}).on('click', ".btn-del", function () {//删除
				var id = $('#ids').val();
				Layer.confirm('确定删除当前产品吗？', {
					btn   : ['确定', '取消'],
					title : '提示',
				}, function (index, layero) {
					Fast.api.ajax("qingdongams/parts/parts/del?ids=" + id, function (data, ret) {
						if (ret.code == 1) {
							Layer.close(index);
							parent.location.reload();
						}
					}, function (data, ret) {
					});

				});
			});

			//库存变更记录
			var table = $("#stock_table");
			table.bootstrapTable({
				url          : 'qingdongams/parts/parts/get_stock_record' + location.search,
				pk           : 'id',
				sortName     : 'id',
				operate      : false,
				commonSearch : false,
				search       : false,
				visible      : false,
				showColumns  : false,
				showExport   : false,
				showToggle   : false,
				columns      : [
					[

						{field: 'create_staff.name',title: __('提交人'),operate: false,},

						{field: 'odd_numbers',title: '申请编号',fixedColumns: true,formatter: function (value, row, index) {
								return "<a href='javascript:void(0);' data-id='" + row.id + "' class='show-log_info'>" + value + "</a>";
							},operate: false
						},
						{field: 'type',title: '出库/入库',operate: false,formatter: Table.api.formatter.status,
							searchList: {1: __('入库'), 2: __('出库')}},
						{field: 'number', title: '数量', operate: false},
						{field: 'storage_time', title: '出入库时间', operate: false},
						{field: 'createtime',title: __('创建时间'),operate: false},
					]
				]
			});

			//库存审批记录
			var table1 = $("#examine_table");
			table1.bootstrapTable({
				url: 'qingdongams/parts/parts/logs' + location.search,
				sortName: 'id',
				columns: [
					[
						{field: 'state', checkbox: true,},
						{
							field: 'parts.create_staff.name',
							title: __('提交人'),
							operate: false,
							searchList: $.getJSON("qingdongams/customer/customer/stafflist")
						},

						{
							field: 'parts.odd_numbers',
							title: '申请编号',
							fixedColumns: true,
							formatter: function (value, row, index) {

								return "<a href='javascript:void(0);' data-id='" + row.parts.id + "' class='show-log_info'>" + value + "</a>";
							},
							operate: 'LIKE'
						},
						{
							field: 'parts.type',
							title: '出库/入库',
							operate: '=',
							formatter: Table.api.formatter.status,
							searchList: {1: __('入库'), 2: __('出库')}
						},
						{field: 'parts.storage_time', title: '出入库时间', operate: false},
						{field: 'check_staff.name', title: __('审批人'), operate: false},
						{
							field: 'status',
							title: __('审批状态'),
							operate: '=',
							formatter: Table.api.formatter.status,
							searchList: {0: __('待审批'), 1: __('审批通过'), 2: __('审批拒绝'), 3: __('撤销')}
						},
						{
							field: 'createtime',
							title: __('创建时间'),
							formatter: Table.api.formatter.datetime,
							operate: 'RANGE',
							addclass: 'datetimerange'
						},
					]
				],
				//启用普通表单搜索
				commonSearch: true,
				searchFormVisible: false,
				search      : false,
			});

			$(document).on('click', '.show-log_info', function (data) {
				var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
				var options = {
					shadeClose: false,
					shade: [0.3, '#393D49'],
					area: area,
					end: function (value) {
						//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
						$(".btn-refresh").trigger("click");
					}
				};
				Fast.api.open("qingdongams/parts/parts/log_info?ids=" + $(this).data('id'), '出入库详情', options);
			});

			$('.btn-ajax').data("success", function(data, ret){
				//处理回调
				location.reload();
			})
			// 为表格绑定事件
			Table.api.bindevent(table);
			Controller.api.bindevent();
		},
		api    : {
			bindevent : function () {
				$(document).on('click', '.product', function (data) {
					var product_id = $(this).attr('data-value')
					var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
					var options = {
						shadeClose : false,
						shade      : [0.3, '#393D49'],
						area       : area,
						callback   : function (value) {
							//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
							console.log(value);
						}
					};
					Fast.api.open("qingdongams/parts/parts/detail?ids=" + product_id, '备件详情', options);
				});
				Form.api.bindevent($("form[role=form]"));
			}
		}
	};
	return Controller;
});